import { ResPage } from "@/api/interface";
import { Tag } from "@/api/interface/tag";
import { TagState } from "@/redux/interface";
import * as types from "@/redux/mutation-types";
import produce from "immer";

export type TagAction =
	| {
			type: "SET_TAG_LIST";
			payload: ResPage<Tag>;
	  }
	| {
			type: "SET_TAG_DETAIL";
			payload: Tag;
	  };

const tagState: TagState = {
	tagList: [],
	tagDetail: {} as Tag,
	tagTotal: 0
};

export default function tag(state = tagState, action: TagAction) {
	return produce(state, draftState => {
		switch (action.type) {
			case types.SET_TAG_LIST:
				draftState.tagList = action.payload.rows;
				draftState.tagTotal = action.payload.total;
				break;
			case types.SET_TAG_DETAIL:
				draftState.tagDetail = action.payload;
				break;
			default:
				break;
		}
	});
}
